import type { INavigatorStyleApplyConfig } from './types'
import { ThemeVarService } from '../../lib/abstract/style/theme_var/service'
import { ICON_IDS } from '../../lib/icons/types/constants'
import {
  BodyClasses,
  PROJECT_PREFIX,
} from '../../types/constants'
import { StylesService } from '../../types/constants/styles'

function baseStyles(): string {
  return `
.meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-left: 20px;
  
  .header {
    display: flex;
    position: relative;
    align-items: center;
    justify-content: space-between;
    
    span, a {
      display: inline-flex;
      align-items: center;
      line-height: 1;
    }
    
    span {
      color: var(--${PROJECT_PREFIX}-theme-text-primary) !important;
    }
    
    a {
      ${StylesService.getInstance().link.default()}
    }
  }
  
  .name {
    font-size: 15px;
    font-weight: 500;
    color: var(--${PROJECT_PREFIX}-theme-accent) !important;
  }
  
  .description {
    font-size: 13px;
    color: var(--${PROJECT_PREFIX}-theme-text-secondary) !important;
  }
  
  .stats {
    font-size: 12px;
    position: relative;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    align-items: center;
    
    .stat {
      display: flex;
      align-items: center;
      color: var(--${PROJECT_PREFIX}-theme-secondary) !important;
      white-space: nowrap !important;
      
      .icon {
        margin-right: 4px;
        font-size: 12px;
      }
      
      span {
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        color: var(--${PROJECT_PREFIX}-theme-text-primary) !important;
      }
    }
  }
}
  `
}

export function styles(config: INavigatorStyleApplyConfig): string {
  const {
    className,
    selector,
    layout,
    quickSearchTags,
    badgeStyles = '',
    customStyles = '',
  } = config

  return `
    ${selector.scrollElement} {
      &.${className.highlight} {
        animation: highlight-glow 2.5s ease;
      }
    }

    .${className.base} {
      position: fixed;
      top: 0;
      left: 50%;
      transform: ${layout?.transform};
      z-index: 1000;
      background: var(--${PROJECT_PREFIX}-theme-toolbar-bg) !important;
      box-shadow: var(--${PROJECT_PREFIX}-theme-toolbar-shadow-bg) !important;
      border-radius: 0;
      width: ${layout?.width} !important;
      ${layout?.heightOptions?.base ? `height: ${layout?.heightOptions.base} !important;` : ''}
      ${layout?.heightOptions?.max ? `max-height: ${layout?.heightOptions.max} !important;` : ''}
      min-height: ${layout?.heightOptions?.min} !important;
      
      @media (max-width: ${layout?.width}) {
        width: 100% !important;
      }
      
      ${customStyles}
      
      &.${BodyClasses.IS_EMPTY} {
        display: none !important;
      }
      
      &.position-bottom {
        top: auto;
        bottom: 20px;
      }
      
      &:not(.position-bottom) {
        top: 0;
      }
      
      /* 徽章样式 */
      .badge {
        margin-left: 8px;
        padding: 2px 6px;
        border-radius: 10px;
        font-size: 11px;
        font-weight: 500;

        ${badgeStyles}
      }

      .current-item {
        display: flex;
        align-items: center;
        padding: 12px 16px;
        cursor: pointer;
        user-select: none;
        
        .badge {
          vertical-align: middle;
        }
        
        .avatar {
          width: 36px;
          height: 36px;
          border-radius: 50%;
          object-fit: cover;
          margin: unset !important;
          margin-right: 12px;
          border: 2px solid var(--${PROJECT_PREFIX}-theme-primary) !important;
        }
        
        ${baseStyles()}
        
        .navigator-info {
          padding: 0 8px !important;

          span {
            font-size: 13px;
            color: var(--${PROJECT_PREFIX}-theme-text-secondary) !important;
          }
        }
        
        .toggle {
          background-image: var(${ThemeVarService.getInstance().generateIcon(ICON_IDS.navigation.arrowsDown)}) !important;
          background-repeat: no-repeat !important;
          background-size: 24px !important;
          width: 24px !important;
          height: 24px !important;
          background-position: center !important;
        }
      }
      
      .dropdown {
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease;
        border-radius: 0;
        background: var(--${PROJECT_PREFIX}-theme-toolbar-bg) !important;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;

        .sort-box {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          padding: 0 12px 8px;
        }
        
        .result-count {
          padding: 8px 12px;
          font-size: 13px;
          color: var(--${PROJECT_PREFIX}-theme-text-secondary) !important;
          background: transparent !important;
        }
        
        .list {
          max-height: 60vh;
          overflow-y: auto;
          padding-bottom: 8px;
          
          .item {
            display: flex;
            padding: 25px 16px;
            cursor: pointer;
            align-items: center !important;
            position: relative !important;
            border-bottom: 1px solid var(--${PROJECT_PREFIX}-theme-code-toolbar-divider) !important;
            &:last-child {
              border-bottom: none !important;
            }
            
            &:hover {
              background: var(--${PROJECT_PREFIX}-theme-dropdown-bg-hover) !important;
            }
            
            .avatar {
              width: 32px;
              height: 32px;
              border-radius: 50%;
              margin: unset !important;
              margin-right: 12px;
              flex-shrink: 0;
            }
            
            ${baseStyles()}
            .meta {
              .tags {
                display: flex;
                align-items: center;
                justify-content: flex-end;
                gap: 10px;

                /* 动态生成标签样式 */
                ${quickSearchTags?.map(tag => `
                  .tag-${tag.type} {
                    background: ${tag.bgColor} !important;
                    color: ${tag.textColor} !important;
                    padding: 4px 8px !important;
                    border-radius: 12px !important;
                    border: 1px solid var(--${PROJECT_PREFIX}-theme-code-toolbar-divider) !important;
                    font-size: 12px;
                    &:hover {
                      background: ${tag.hoverBgColor} !important;
                    }
                  }
                `).join('')}
              }
            }
          }
        }
      }
      
      &.expanded {
        overflow: unset !important;
        
        .dropdown {
          max-height: ${layout?.heightOptions?.dropdownMax};
        }
        .toggle {
          transform: rotate(180deg);
        }
      }
    }
    
    @media (max-width: 768px) {
      .${className} {
        width: 95%;
        
        &.position-bottom {
          bottom: 10px;
        }
        
        &:not(.position-bottom) {
          top: 10px;
        }
        
        .current-item {
          padding: 8px 12px;
        }
      }
    }
  `
}
